﻿using SIG.DAL.Dapper.Model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIG.DAL.Dapper.Repositories
{

    public class PhotoRepository
    {
        private DbConnection _connection;
        public IEnumerable<Photo> GetActiveItems(int albumId)
        {
            IEnumerable<Photo> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<Photo>("where Active = 1 and AlbumId = @AlbumId", new { AlbumId = albumId });
            }
            return result;
        }

        public IEnumerable<Photo> GetListPagedItems(int pageIndex, int pageSize, string keyword, int parentId, out int totalCount)
        {
            IEnumerable<Photo> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                keyword = !string.IsNullOrEmpty(keyword) ? keyword.Replace("[", "[[]").Replace("%", "[%]") : keyword;
                string likeKeyword = "%" + keyword + "%";

                var sql = "where 1=1 ";

                if (!string.IsNullOrEmpty(keyword))
                {
                    sql = sql + " and Title like @Keyword ";
                }
                if (parentId > 0)
                {
                    sql = sql + " and AlbumId = @AlbumId ";
                }

                totalCount = _connection.RecordCount<Photo>(sql,
                    new { Keyword = likeKeyword, AlbumId = parentId });
                result = _connection.GetListPaged<Photo>(pageIndex, pageSize, sql, "Importance desc",
                    new { Keyword = likeKeyword, AlbumId = parentId });

            }
            return result;
        }

        public int? Create(Photo model)
        {
            int? result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Insert(model);
            }
            return result;
        }

        public int Update(Photo model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Update(model);
            }
            return result;
        }
        public int Delete(Photo model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete(model);
            }
            return result;
        }

        public int Delete(int id)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete<Photo>(id);
            }
            return result;
        }

        public Photo GetById(int id)
        {
            Photo result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Get<Photo>(id);
            }
            return result;
        }
    }
}
